__all__ = [
    "default_call_back",
    "all_data_call_back",
    "semicolon_call_back",
    "wf_list_callback",
    "list_call_back",
    "space_call_back",
]

import re


def default_call_back(key, value):
    """
    默认的解析回调
    :param key:
    :param value:
    :return:
    """
    result = ""
    if isinstance(value, list):
        for val in value:
            val = val.replace("\r", "").replace("\n", "").strip()
            if val != "":
                result = result + val
    else:
        result = value
    return result


def all_data_call_back(key, value):
    """
    不做任何处理 直接拼接数据
    :param key:
    :param value:
    :return:
    """
    result = ""
    if isinstance(value, list):
        for val in value:
            if val != "":
                result = result + val
    else:
        result = value
    return result


def semicolon_call_back(key, value):
    """
    以分号拼接数据
    :param key:
    :param value:
    :return:
    """
    result = ""
    if isinstance(value, list):
        for val in value:
            val = val.replace("\r", "").replace("\n", "").strip()
            if val != "":
                result = result + val + ";"
    else:
        result = value
    if isinstance(result, str):
        result = result.rstrip(";")
    return result


def list_call_back(key, value):
    """
    返回list格式
    :param key:
    :param value:
    :return:
    """
    return value


def wf_list_callback(key, value):
    """
    专用的数据返回格式
    :param key:
    :param value:
    :return:
    """
    result = ""
    if isinstance(value, list):
        for val in value:
            val = val.replace("\r\n", ";")
            val = val.replace("\r", "").replace("\n", "").strip()
            if val != "":
                result = result + val + ";"
    else:
        result = value
    if isinstance(result, str):
        result = result.rstrip(";")
    return result


def space_call_back(key, value):
    """
    针对多个空格不直接去掉, 而是仅保留一个, 仅ZCN对00638使用
    :param key:
    :param value:
    :return:
    """
    result = ""
    if isinstance(value, list):
        rule = " +$"
        rule_s = "^ +"
        for val in value:
            val = val.replace("\r", "").replace("\n", "").replace("\t", "")
            if val != "":
                if val.endswith(" "):
                    val = re.sub(rule, "★★★", val)
                if val.startswith(" "):
                    val = re.sub(rule_s, "★★★", val)
                val = val.strip()
                result = result + val
    else:
        result = value
    result = result.replace("★★★", " ").strip()
    return result